package com.kinyx.framework.db.entities;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;

import com.kinyx.framework.annotation.RandomIgnore;
import com.kinyx.framework.db.annotation.Comment;
import com.kinyx.framework.db.annotation.Order;
import com.kinyx.framework.db.vo.TableInfo;
import com.kinyx.framework.reflect.KFieldUtils;
import com.kinyx.framework.utils.KListUtils;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public class MappedSuperclassID implements I_ID {

	@Id
	@Column(nullable = false, updatable = false, length = 32)
	@Comment("主键")
	@Order(-10000)
	@RandomIgnore
	private String id;

	@Override
	public String data(final TableInfo tableInfo) {
		final StringBuilder sb = new StringBuilder().append("\"").append(tableInfo.getTableName()).append("\"").append(":");
		sb.append("{");
		sb.append(KListUtils.join(tableInfo.getColumnInfos(), ",", (columnInfo, index) -> {
			return "\"" + columnInfo.getColumnName() + "\"" + ":" + "\"" + KFieldUtils.get(this, columnInfo.getField()) + "\"";
		}));
		sb.append("}");
		return sb.toString();
	}

}
